-- map.lua

-- Functions available: (brackets indicate optional arguments)
-- RG_map.createMap(filename, height, width)
-- RG_map.writeTile(tile)
-- RG_map.mapExists(filename)
--
-- Tiles are passed as tables of numbers. Index 1 is the type and index 2 is the
-- altitude.

createTestMap = function()
    tile = 0
    RG_map.createMap("maps/testmap.rgm", 150, 150)
    for x = 1,150 do
        for y = 1,150 do
            tile = y / 6
            if tile == 0 then
                tile = grass
            elseif tile == 1 then
                tile = darkGrass
            elseif tile == 2 then
                tile = darkerGrass
            elseif tile == 3 then
                tile = desert
            elseif tile == 4 then
                tile = darkDesert
            elseif tile == 5 then
                tile = darkerDesert
            end
            RG_map.writeTile({tile,0})
        end
    end
end

createRandomMap = function()
    tileLeft = 0
    previousRow = {}
    if not RG_map.mapExists("maps/random.rgm") then
        RG_map.createMap("maps/random.rgm", 250, 250)
        for y = 1,250 do
            for x = 1,250 do
                tileBelow = previousRow[x] or 0
                tile = math.mod((tileLeft + tileBelow + math.random(0,5)), 3)
                tileLeft = tile
                previousRow[x] = tile
                if tile == 0 then
                    tile = grass
                elseif tile == 1 then
                    tile = darkGrass
                elseif tile == 2 then
                    tile = darkerGrass
                elseif tile == 3 then
                    tile = darkerDesert
                elseif tile == 4 then
                    tile = darkDesert
                elseif tile == 5 then
                    tile = desert
                end
                RG_map.writeTile({tile,0})
            end
        end
    end
end

--createTestMap()
createRandomMap()
